package com.wxg.tcp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class talkrecevie implements Runnable {
    DatagramSocket socket=null;
    private int port;
    private String magsfrom;

    public talkrecevie(int port,String magsfrom) {
        this.port = port;
        this.magsfrom=magsfrom;
        try {
            socket=new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        while (true){
            try {
                byte[] bytes = new byte[1024];
                DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
                socket.receive(packet);
                byte[] data = packet.getData();
                String s = new String(data, 0, data.length);
                System.out.println(magsfrom+":"+s);
                if (s.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();

    }
}
